package com.daffodil.workable.factory.strategy;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import javax.validation.constraints.NotNull;

import org.springframework.util.Assert;

import com.daffodil.workable.entity.WorkableNode;
import com.daffodil.workable.entity.WorkableResult;

/**
 * -任务工作流-默认处理节点
 * @author yweijian
 * @date 2022年4月8日
 * @version 1.0
 * @description
 */
public abstract class DefaultWorkableStrategy implements WorkableStrategy {

    @Override
    public WorkableResult run(@NotNull String traceId, @NotNull WorkableNode workableNode, WorkableResult lastResult) {
        Assert.notNull(traceId, "The traceId is not allowed null");
        Assert.notNull(workableNode, "The WorkableNode is not allowed null");
        try {
            TimeUnit.MILLISECONDS.sleep(500 - System.currentTimeMillis() % 500);
        } catch (InterruptedException ignore) { }
        
        WorkableResult curtResult = new WorkableResult();
        curtResult.setTraceId(traceId);
        curtResult.setTaskId(workableNode.getTaskId());
        curtResult.setNodeId(workableNode.getNodeId());
        curtResult.setStatus(true);
        curtResult.setMessage("OK");
        curtResult.setCreateTime(new Date());
        return this.chain(workableNode, curtResult, lastResult);
    }

    /**
     * 
     * @param workableNode
     * @param curtResult 当前节点执行结果
     * @param lastResult 上一节点执行结果
     * @return
     */
    protected abstract WorkableResult chain(WorkableNode workableNode, WorkableResult curtResult, WorkableResult lastResult);
    
}
